package com.oocl.producer.producer;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;

import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.command.ActiveMQQueue;

/**
 * 以Json存入MQ
 * @author GANAB
 *
 */
public class BookProducer {
	private static final String URL = "tcp://10.222.29.179:61616";
	private ConnectionFactory factory = null;
	private Destination queue = null;
	private Connection con = null;
	private Session session = null;
	private MessageProducer producer = null;
	public BookProducer(String queueName) {
		factory = new ActiveMQConnectionFactory(URL);
		queue = new ActiveMQQueue(queueName);
		try {
			con = factory.createConnection();
			con.start();
			session = con.createSession(false, Session.CLIENT_ACKNOWLEDGE);
			producer = session.createProducer(queue);
		} catch (JMSException e) {
			e.printStackTrace();
		}
	}
	
	public void sendMsg(String msg) throws JMSException{
		TextMessage message = session.createTextMessage(msg);
		producer.send(message);
		message.acknowledge();
	}
	
	public void close() throws JMSException{
		if(producer!=null){
			producer.close();
		}
		if(session!=null){
			session.close();
		}
		if(con!=null){
			con.close();
		}
	}
	
}
